package com.example.assetmanagement.controller;

import com.example.assetmanagement.common.ApiResponse;
import com.example.assetmanagement.service.BackupService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/system/backup")
@RequiredArgsConstructor
public class BackupController {

    private final BackupService backupService;

    @PostMapping("/create")
    public ApiResponse<?> create(@RequestParam(required = false) Long userId) throws IOException, InterruptedException {
        backupService.createBackup(userId);
        return ApiResponse.success(null);
    }

    @PostMapping("/import")
    public ApiResponse<?> importSql(@RequestPart("file") MultipartFile file,
                                    @RequestParam(required = false) Long userId) throws IOException, InterruptedException {
        backupService.importBackup(file, userId);
        return ApiResponse.success(null);
    }

    @PostMapping("/{id}/restore")
    public ApiResponse<?> restore(@PathVariable Long id) throws IOException, InterruptedException {
        backupService.restoreBackup(id);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/{id}")
    public ApiResponse<?> delete(@PathVariable Long id) {
        backupService.deleteBackup(id);
        return ApiResponse.success(null);
    }

    @GetMapping("/list")
    public ApiResponse<Map<String, Object>> list(@RequestParam(defaultValue = "1") int page,
                                                 @RequestParam(defaultValue = "10") int size) {
        Map<String, Object> res = Map.of(
                "total", backupService.count(),
                "list", backupService.list(page, size)
        );
        return ApiResponse.success(res);
    }
} 